/*
 * jlb
 */
package com.jlb.count.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.baidu.fsg.uid.UidGenerator;
import com.jlb.core.base.BaseDAO;
import com.jlb.core.base.BaseSVImpl;
import com.jlb.core.entity.Page;
import com.jlb.core.exceptions.BaseException;
import com.jlb.core.exceptions.CountException;
import com.jlb.count.dao.CountDAO;
import com.jlb.count.entity.Count;
import com.jlb.count.entity.CountState;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.map.HashedMap;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("countSV")
@Slf4j
public class CountSVImpl extends BaseSVImpl<Count, Long> implements CountSV {

    @Autowired
    private CountDAO countDAO;

    @Resource
    private UidGenerator uidGenerator;

    @Override
    protected BaseDAO getBaseDAO() {
        return countDAO;
    }

    /**
     * 保存account对象
     *
     * @param entity 实体
     * @throws BaseException
     */
    @Override
    public void save(Count entity) throws BaseException {
        super.save(entity);
    }

    /**
     * 加载一个对象Count
     *
     * @param id
     * @return Count
     */
    @Override
    public Count load(Long id) {
        if (id == null) {
            throw new CountException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }

        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        return countDAO.load(param);
    }

    /**
     * 加载一个对象Count 通过id
     *
     * @param id
     * @return Count
     */
    @Override
    public Count loadById(Long id) {
        if (id == null) {
            throw new CountException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        return countDAO.loadById(id);
    }


    /**
     * 根据主键id,oldStates 共同更新 Count 的状态到newState状态
     *
     * @param id
     * @param newState  新状态
     * @param oldStates 旧状态集合
     */
    @Override
    public void updateStateById(Long id, CountState newState, CountState... oldStates) {
        if (id == null) {
            throw new CountException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        if (newState == null) {
            throw new CountException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        if (oldStates == null) {
            throw new CountException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        countDAO.updateStateById(id, new Date(), newState, oldStates);
    }

    /**
     * 根据主键id 更新 Count 的状态到另一个状态
     *
     * @param id
     * @param state 状态
     */
    @Override
    public void updateById(Long id, CountState state) {
        if (id == null) {
            throw new CountException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        countDAO.updateById(id, state, new Date());
    }


    /**
     * 删除对象Count
     *
     * @param id
     * @return Count
     */
    @Override
    public void delete(Long id) {
        if (id == null) {
            throw new CountException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        countDAO.delete(param);
    }


    /**
     * 查询Count分页
     *
     * @param count  对象
     * @param offset 查询开始行
     * @param limit  查询行数
     * @return List<Count>
     */
    @Override
    public List<Count> list(Count count, int offset, int limit) {
        if (offset < 0) {
            offset = 0;
        }

        if (limit < 0) {
            limit = Page.limit;
        }

        Map<String, Object> map = null;
        if (count != null) {
            map = JSON.parseObject(JSON.toJSONString(count, SerializerFeature.WriteDateUseDateFormat));
        } else {
            map = new HashMap<>();
        }
        return countDAO.list(map, new RowBounds(offset, limit));
    }

    @Override
    public int count(Count count) {
        Map<String, Object> map = null;
        if (count != null) {
            map = JSON.parseObject(JSON.toJSONString(count, SerializerFeature.WriteDateUseDateFormat));
        } else {
            map = new HashMap<>();
        }
        return countDAO.count(map);
    }

    /**
     * 查询Count分页
     *
     * @param id
     * @param offset 查询开始行
     * @param limit  查询行数
     * @return List<Count>
     */
    @Override
    public List<Count> list(Long id, int offset, int limit) {
        if (offset < 0) {
            offset = 0;
        }

        if (limit < 0) {
            limit = Page.limit;
        }
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        return countDAO.list(param, new RowBounds(offset, limit));
    }

    @Override
    public int count(Long id) {
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        return countDAO.count(param);
    }


    @Override
    public void saveOrUpdate(Count entity) throws BaseException {
        Count count = countDAO.load(new HashedMap());
        if (count != null) {
            count.setCostJlb(String.valueOf(Integer.parseInt(count.getCostJlb()) + Integer.parseInt(entity.getCostJlb())));
            count.setTotalJlb(String.valueOf(Integer.parseInt(count.getTotalJlb()) + Integer.parseInt(entity.getTotalJlb())));
            count.setTotalOre(String.valueOf(Integer.parseInt(count.getCostOre()) + Integer.parseInt(entity.getCostOre())));
            count.setCostOre(String.valueOf(Integer.parseInt(count.getCostOre()) + Integer.parseInt(entity.getCostOre())));
            super.saveOrUpdate(count);
        } else {
            super.saveOrUpdate(entity);
        }
    }
}
